from os import error
import easygui as g
import urllib.request as request

def main():
    # g.msgbox(msg="xxxxxxx",title="please")
    # g.ccbox(msg="xxxxxxx",title="please")
    
    fieldValues = g.multenterbox(msg="请填写喵的尺寸", title="下载一只猫", fields=('宽：', '高：'))
    print(fieldValues)
    height = 0
    width = 0

    try:
        width = int(fieldValues[0])
        height = int(fieldValues[1])
    except Exception as err:
        g.msgbox("高度或宽度必须为整数！")
        fieldValues = g.multenterbox(
            msg="请填写喵的尺寸", title="下载一只猫", fields=('宽：', '高：'))
    if height != 0 and width != 0:
        url = "http://placekitten.com/g/%d/%d" % (width, height)
        print("url:", url)
        response = request.urlopen(url)
        data = response.read()
        filepath = g.diropenbox("请选择存放猫的文件夹")
        if filepath:
            filename = "%s/cat_%d_%d.jpg" %(filepath,width,height)
        else:
            filename = "cat_%d_%d.jpg" %(width,height)
        print(filename)
        # print(response.getcode())
        with open(filename, 'wb') as f:
            f.write(data)

if __name__=="__main__":
    main()
